﻿using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using clf.weighing.entity;

namespace weighing
{
    public class SystemDefaultValue
    {

        #region 权限有关

        public static List<Permission> Permissions = new List<Permission>()
        {
            new Permission(){id=1,name="系统设置",key="setting",descript="系统设置",status=1, parent = 0,level = 1},
            new Permission(){id=2,name="服务器设置",key="setting:server",descript="数据上传的服务器",status=1, parent = 0,level = 2},
            new Permission(){id=3,name="初始化设置",key="setting:init",descript="",status=1, parent = 1,level = 2},
            new Permission(){id=4,name="初始化系统名称",key="setting:init:name",descript="",status=1, parent = 3,level = 3},
            new Permission(){id=5,name="初始化使用单位",key="setting:init:company",descript="",status=1, parent = 3,level = 3},
            new Permission(){id=6,name="截图设置",key="bill:img:setting",descript="",status=1, parent =1,level = 2},
            new Permission(){id=7,name="Ic读卡器设置",key="setting:Ic:reader",descript="",status=1, parent =1,level = 2},

            new Permission(){id=10,name="磅单管理",key="bill:manager",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=11,name="磅单设置",key="bill:setting",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=12,name="补录磅单",key="bill:bulu",descript="bill:bulu",status=1, parent = 10,level = 2},
            new Permission(){id=13,name="修改磅单",key="bill:edit",descript="bill:edit",status=1, parent =10,level = 2},
            new Permission(){id=14,name="删除磅单",key="bill:del:finish",descript="",status=1, parent = 10,level =2},
            new Permission(){id=15,name="删除未完成的磅单",key="bill:del:no",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=16,name="清除内倒磅单",key="bill:clear:self",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=17,name="清除外来磅单",key="bill:clear:foreign",descript="",status=1, parent = 10,level = 2},

            new Permission(){id=18,name="补打磅单",key="bill:muitl:print",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=19,name="取消出场",key="bill:cancel:out",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=20,name="采购过磅设置",key="bill:in:setting",descript="",status=1, parent = 10,level = 2},
            new Permission(){id=21,name="销售过磅设置",key="bill:out:setting",descript="",status=1, parent = 10,level = 2},

            new Permission(){id=30,name="设备管理",key="device",descript="",status=1, parent = 0,level = 1},
            new Permission(){id=31,name="摄像头",key="device:camera",descript="",status=1, parent = 30,level = 2},
            new Permission(){id=32,name="添加",key="device:camera:add",descript="",status=1, parent = 31,level = 3},
            new Permission(){id=33,name="修改",key="device:camera:edit",descript="",status=1, parent = 31,level = 3},
            new Permission(){id=34,name="删除",key="device:camera:del",descript="",status=1, parent = 31,level = 3},

            new Permission(){id=40,name="磅称管理",key="device:scale",descript="",status=1, parent = 30,level = 2},
            new Permission(){id=41,name="添加",key="device:scale:add",descript="",status=1, parent = 40,level = 3},
            new Permission(){id=42,name="修改",key="device:scale:edit",descript="",status=1, parent = 40,level = 3},
            new Permission(){id=43,name="删除",key="device:scale:del",descript="",status=1, parent = 40,level = 3},

            new Permission(){id=45,name="室外高频读卡器管理",key="device:hight:ic:reader",descript="",status=1, parent = 30,level = 2},
            new Permission(){id=46,name="添加",key="device:hight:ic:reader:add",descript="",status=1, parent = 45,level = 3},
            new Permission(){id=47,name="修改",key="device:hight:ic:reader:edit",descript="",status=1, parent = 45,level = 3},
            new Permission(){id=48,name="删除",key="device:hight:ic:reader:del",descript="",status=1, parent = 45,level = 3},

            new Permission(){id=50,name="网口扫码器",key="device:netScanner",descript="",status=1, parent = 30,level = 2},
            new Permission(){id=51,name="添加",key="device:netScanner:add",descript="",status=1, parent = 51,level = 3},
            new Permission(){id=52,name="修改",key="device:netScanner:edit",descript="",status=1, parent = 51,level = 3},
            new Permission(){id=53,name="删除",key="device:netScanner:del",descript="",status=1, parent = 51,level = 3},

            new Permission(){id=60,name="基础数据",key="basedata",descript="",status=1, parent = 0,level = 1},
            new Permission(){id=61,name="车辆管理",key="basedata:car",descript="",status=1, parent = 60,level = 2},
            new Permission(){id=62,name="添加",key="basedata:car:add",descript="",status=1, parent = 61,level = 3},
            new Permission(){id=63,name="修改",key="basedata:car:edit",descript="",status=1, parent = 61,level = 3},
            new Permission(){id=64,name="删除",key="basedata:car:del", descript="",status=1, parent = 61,level = 3},
            new Permission(){id=65,name="发IC卡",key="basedata:car:icdata", descript="",status=1, parent = 61,level = 3},

            new Permission(){id=66,name="磅房管理",key="basedata:scale:house",descript="",status=1, parent = 60,level = 2},
            new Permission(){id=67,name="添加",key="basedata:scale:house:add",descript="",status=1, parent = 66,level = 3},
            new Permission(){id=68,name="修改",key="basedata:scale:house:edit",descript="",status=1, parent = 66,level = 3},
            new Permission(){id=69,name="删除",key="basedata:scale:house:del", descript="",status=1, parent = 66,level = 3},

            new Permission(){id=70,name="物料分类",key="basedata:cate",descript="",status=1, parent = 60,level = 2},
            new Permission(){id=71,name="添加",key="basedata:cate:add",descript="",status=1, parent = 70,level = 3},
            new Permission(){id=72,name="修改",key="basedata:cate:edit",descript="",status=1, parent = 70,level = 3},
            new Permission(){id=73,name="删除",key="basedata:cate:del", descript="",status=1, parent = 70,level = 3},

            new Permission(){id=80,name="物料管理",key="basedata:material",descript="",status=1, parent = 60,level = 2},
            new Permission(){id=81,name="添加",key="basedata:material:add",descript="",status=1, parent = 80,level = 3},
            new Permission(){id=82,name="修改",key="basedata:material:edit",descript="",status=1, parent = 80,level = 3},
            new Permission(){id=83,name="删除",key="basedata:material:del", descript="",status=1, parent = 80,level = 3},
            new Permission(){id=84,name="价格管理",key="basedata:material:price", descript="",status=1, parent = 80,level = 3},

            new Permission(){id=90,name="客户信息",key="basedata:company",descript="",status=1, parent = 60,level = 2},
            new Permission(){id=91,name="添加",key="basedata:company:add",descript="",status=1, parent = 90,level = 3},
            new Permission(){id=92,name="修改",key="basedata:company:edit",descript="",status=1, parent = 90,level = 3},
            new Permission(){id=93,name="删除",key="basedata:company:del", descript="",status=1, parent = 90,level = 3},


            new Permission(){id=100,name="数据",key="weighing:data",descript="过磅报表",status=1, parent = 0,level = 1},
            new Permission(){id=101,name="采购数据",key="weighing:data:in",descript="",status=1, parent = 100,level = 2},
            new Permission(){id=102,name="销售数据",key="weighing:data:out",descript="",status=1, parent = 100,level = 2},
            new Permission(){id=103,name="导出Excl",key="weighing:data:excport",descript="",status=1, parent = 100,level = 2},


            new Permission(){id=110,name="合同管理",key="constract",descript="合同管理",status=1, parent = 0,level = 1},
            new Permission(){id=111,name="增加",key="constract:add",descript="",status=1, parent = 110,level = 2},
            new Permission(){id=112,name="修改",key="constract:edit",descript="",status=1, parent = 110,level = 2},
            new Permission(){id=113,name="删除",key="constract:del",descript="",status=1, parent = 110,level = 2},
            new Permission(){id=114,name="审核",key="constract:audit",descript="",status=1, parent = 110,level = 2},

        };

        public static List<RolePermission> RolePermissions = new List<RolePermission>()
        {
            new RolePermission(){id= 1,roleId = 2,permissionId=  1},
            new RolePermission(){id= 2,roleId = 2,permissionId=  2},
            new RolePermission(){id= 3,roleId = 2,permissionId=  3},
            new RolePermission(){id= 4,roleId = 2,permissionId=  4},
            new RolePermission(){id= 5,roleId = 2,permissionId=  5},
            new RolePermission(){id= 6,roleId = 2,permissionId=  6},

            new RolePermission(){id= 10,roleId = 2,permissionId=  10},
            new RolePermission(){id= 11,roleId = 2,permissionId=  11},
            new RolePermission(){id= 12,roleId = 2,permissionId=  12},
            new RolePermission(){id= 13,roleId = 2,permissionId=  13},
            new RolePermission(){id= 14,roleId = 2,permissionId=  14},
            new RolePermission(){id= 15,roleId = 2,permissionId=  15},
            new RolePermission(){id= 16,roleId = 2,permissionId=  16},
            new RolePermission(){id= 17,roleId = 2,permissionId=  17},

            new RolePermission(){id= 18,roleId = 2,permissionId=  18},
            new RolePermission(){id= 19,roleId = 2,permissionId=  19},
            new RolePermission(){id= 20,roleId = 2,permissionId=  20},
            new RolePermission(){id= 21,roleId = 2,permissionId=  21},

            new RolePermission(){id= 30,roleId = 2,permissionId=  30},
            new RolePermission(){id= 31,roleId = 2,permissionId=  31},
            new RolePermission(){id= 32,roleId = 2,permissionId=  32},
            new RolePermission(){id= 33,roleId = 2,permissionId=  33},
            new RolePermission(){id= 34,roleId = 2,permissionId=  34},

            new RolePermission(){id= 40,roleId = 2,permissionId=  40},
            new RolePermission(){id= 41,roleId = 2,permissionId=  41},
            new RolePermission(){id= 42,roleId = 2,permissionId=  42},
            new RolePermission(){id= 43,roleId = 2,permissionId=  43},

            new RolePermission(){id= 45,roleId = 2,permissionId=  45},
            new RolePermission(){id= 46,roleId = 2,permissionId=  46},
            new RolePermission(){id= 47,roleId = 2,permissionId=  47},
            new RolePermission(){id= 48,roleId = 2,permissionId=  48},

            new RolePermission(){id= 50,roleId = 2,permissionId=  50},
            new RolePermission(){id= 51,roleId = 2,permissionId=  51},
            new RolePermission(){id= 52,roleId = 2,permissionId=  52},
            new RolePermission(){id= 53,roleId = 2,permissionId=  53},
            new RolePermission(){id= 54,roleId = 2,permissionId=  54},
            new RolePermission(){id= 55,roleId = 2,permissionId=  55},

            new RolePermission(){id= 60,roleId = 2,permissionId=  60},
            new RolePermission(){id= 61,roleId = 2,permissionId=  61},
            new RolePermission(){id= 62,roleId = 2,permissionId=  62},
            new RolePermission(){id= 63,roleId = 2,permissionId=  63},

            new RolePermission(){id= 64,roleId = 2,permissionId=  64},
            new RolePermission(){id= 65,roleId = 2,permissionId=  65},
            new RolePermission(){id= 66,roleId = 2,permissionId=  66},
            new RolePermission(){id= 67,roleId = 2,permissionId=  67},

            new RolePermission(){id= 70,roleId = 2,permissionId=  70},
            new RolePermission(){id= 71,roleId = 2,permissionId=  71},
            new RolePermission(){id= 72,roleId = 2,permissionId=  72},
            new RolePermission(){id= 73,roleId = 2,permissionId=  73},
            new RolePermission(){id= 74,roleId = 2,permissionId=  74},

            new RolePermission(){id= 80,roleId = 2,permissionId=  80},
            new RolePermission(){id= 81,roleId = 2,permissionId=  81},
            new RolePermission(){id= 82,roleId = 2,permissionId=  82},
            new RolePermission(){id= 83,roleId = 2,permissionId=  83},
            new RolePermission(){id= 84,roleId = 2,permissionId=  84},

            new RolePermission(){id= 90,roleId = 2,permissionId=  90},
            new RolePermission(){id= 91,roleId = 2,permissionId=  91},
            new RolePermission(){id= 92,roleId = 2,permissionId=  92},
            new RolePermission(){id= 93,roleId = 2,permissionId=  93},

            new RolePermission(){id= 100,roleId = 2,permissionId=  100},
            new RolePermission(){id= 101,roleId = 2,permissionId=  101},
            new RolePermission(){id= 102,roleId = 2,permissionId=  102},
            new RolePermission(){id= 103,roleId = 2,permissionId=  103},

            new RolePermission(){id= 110,roleId = 2,permissionId=  110},
            new RolePermission(){id= 111,roleId = 2,permissionId=  111},
            new RolePermission(){id= 112,roleId = 2,permissionId=  112},
            new RolePermission(){id= 113,roleId = 2,permissionId=  113},
            new RolePermission(){id= 114,roleId = 2,permissionId=  114},
        };

        public static List<Role> Roles = new List<Role>()
        {
            new Role(){ id = 1,name="超级管理员",descript="系统开发者",status=1,type=100,isSevered=true},
            new Role(){ id = 2,name="管理员",descript="",status=1,type=2,isSevered=true},
            new Role(){ id = 3,name="过磅员",descript="",status=1,type=1,isSevered=true},
        };

        #endregion


        #region LED
        public static List<LedBrand> LedBrands = new List<LedBrand>()
        {
            new LedBrand(){id = 1,brandName = "上海灵信", type =1 ,isDelete = 0,addTime=DateTime.Now},
        };
        #endregion

        #region 磅称
        public static List<ScaleBrand> ScaleBrands = new List<ScaleBrand>()
        {
            new ScaleBrand(){id = 1,brandName = "上海耀华", type =1 ,isDelete = 0,addTime=DateTime.Now},
            new ScaleBrand(){id = 2,brandName = "宁波柯力", type =2 ,isDelete = 0,addTime=DateTime.Now},
            new ScaleBrand(){id = 3,brandName = "托利多", type =3 ,isDelete = 0,addTime=DateTime.Now},
            new ScaleBrand(){id = 4,brandName = "赛多利斯", type =4 ,isDelete = 1,addTime=DateTime.Now},
        };

        public static List<ScaleSeries> ScaleSeriesList = new List<ScaleSeries>() {
        new ScaleSeries(){id=1,name="A12E A9+P",brandId=1,AddTime = DateTime.Now,isDelete = 0 },
        new ScaleSeries(){id=2,name="GSI201",brandId=1,AddTime = DateTime.Now,isDelete = 0 },
        new ScaleSeries(){id=3,name="XK3190-A6",brandId=1,AddTime = DateTime.Now,isDelete = 0 },
        new ScaleSeries(){id=4,name="XK3190-A27",brandId=1,AddTime = DateTime.Now,isDelete = 0 },
        new ScaleSeries(){id=5,name="GSI201P",brandId=2,AddTime = DateTime.Now,isDelete = 0 },
        new ScaleSeries(){id=6,name="GSI201P12",brandId=2,AddTime = DateTime.Now,isDelete = 0 },
        };

        #endregion


        private static List<RelayPointVo>? inRelayPointVos;
        /// <summary>
        /// 输入触点名称
        /// </summary>
        public static List<RelayPointVo> InRelayPointVos
        {
            get
            {
                if (inRelayPointVos == null || inRelayPointVos.Count <= 0)
                {
                    //输入触点 IN1
                    inRelayPointVos = new List<RelayPointVo>();
                    for (int i = 1; i <= 8; i++)
                    {
                        inRelayPointVos.Add(new RelayPointVo()
                        {
                            Direction = 0,
                            No = i,
                            DisplayName = "输入触点 IN" + i
                        });
                    }
                }
                return inRelayPointVos;
            }

        }

        private static List<RelayPointVo>? outRelayPointVos;
        /// <summary>
        /// 输入触点名称
        /// </summary>
        public static List<RelayPointVo> OutRelayPointVos
        {
            get
            {
                if (outRelayPointVos == null || outRelayPointVos.Count <= 0)
                {
                    //输入触点 IN1
                    outRelayPointVos = new List<RelayPointVo>();
                    for (int i = 1; i <= 8; i++)
                    {
                        outRelayPointVos.Add(new RelayPointVo()
                        {
                            Direction = 1,
                            No = i,
                            DisplayName = "输出触点 Com/No" + i
                        });
                    }
                }
                return outRelayPointVos;

            }

        }


        #region 485 触点，对应 设备

        private static List<RelaySelectDriveVo>? outRelaySelectDriveVos;

        /// <summary>
        /// 输出触点对应设备
        /// </summary>
        public static List<RelaySelectDriveVo> OutRelaySelectDriveVos
        {
            get
            {
                if (outRelaySelectDriveVos == null || outRelaySelectDriveVos.Count <= 0)
                {
                    outRelaySelectDriveVos = new List<RelaySelectDriveVo>()
                        {
                            new RelaySelectDriveVo() { No = 1, DisplayName = "车头红灯", Key = "Header_Red" ,IsEnable = true},
                            new RelaySelectDriveVo() { No = 2, DisplayName = "车头绿灯", Key = "Header_Green" ,IsEnable = true},
                            new RelaySelectDriveVo() { No = 3, DisplayName = "车头道闸开", Key = "Header_Dz_Open",IsEnable = true },
                            new RelaySelectDriveVo() { No = 4, DisplayName = "车头道闸关", Key = "Header_Dz_Close" ,IsEnable = true },
                            new RelaySelectDriveVo() { No = 0, DisplayName = "车头下秤绿灯", Key = "Header_Down_Green",IsEnable = false },
                            new RelaySelectDriveVo() { No = 0, DisplayName = "车尾下秤绿灯", Key = "Tail_Down_Green" ,IsEnable = false },
                            new RelaySelectDriveVo() { No = 5, DisplayName = "车尾道闸关", Key = "Tail_Dz_Close" ,IsEnable = true},
                            new RelaySelectDriveVo() { No = 6, DisplayName = "车尾道闸开", Key = "Tail_Dz_Open" ,IsEnable = true},
                            new RelaySelectDriveVo() { No = 7, DisplayName = "车尾红灯", Key = "Tail_Red" ,IsEnable = true},
                            new RelaySelectDriveVo() { No = 8, DisplayName = "车尾绿灯", Key = "Tail_Green" ,IsEnable = true},
                        };
                }
                return outRelaySelectDriveVos;

            }

        }


        private static List<RelaySelectDriveVo>? inrelaySelectDriveVos;
        /// <summary>
        /// 输入触点对应设备
        /// </summary>
        public static List<RelaySelectDriveVo> InRelaySelectDriveVos
        {
            get
            {
                if (inrelaySelectDriveVos == null || inrelaySelectDriveVos.Count <= 0)
                {
                    inrelaySelectDriveVos = new List<RelaySelectDriveVo>
                    {
                        new RelaySelectDriveVo() { No = 1, DisplayName = "车头道闸关状态", Key = "Header_Close_Dz" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 2, DisplayName = "车头道闸开状态", Key = "Header_Open_Dz",IsEnable = true },
                        new RelaySelectDriveVo() { No = 3, DisplayName = "车头红外状态", Key = "Header_Hw" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 4, DisplayName = "左测红外状态", Key = "Left_Hw" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 5, DisplayName = "右测红外状态", Key = "Right_Hw" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 6, DisplayName = "车尾红外状态", Key = "Tail_Hw" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 7, DisplayName = "车尾道闸关状态", Key = "Tail_Close_Dz" ,IsEnable = true},
                        new RelaySelectDriveVo() { No = 8, DisplayName = "车尾道闸开状态", Key = "Tail_Open_Dz" ,IsEnable = true},
                    };
                }
                return inrelaySelectDriveVos;

            }

        }

        private static Dictionary<string, int> defaultDic = new Dictionary<string, int>();

        /// <summary>
        /// 系统默认设置的 485 触点和设备的对应
        /// </summary>
        public static Dictionary<string, int> DefaultDevicePointDic
        {
            get
            {
                if (defaultDic == null || defaultDic.Count <= 0)
                {
                    defaultDic = new Dictionary<string, int>();

                    foreach (var initem in InRelaySelectDriveVos)
                    {
                        if (initem.IsEnable) defaultDic.Add(initem.Key, initem.No);
                    }

                    foreach (var outitem in OutRelaySelectDriveVos)
                    {
                        if (outitem.IsEnable) defaultDic.Add(outitem.Key, outitem.No);
                    }
                }

                return defaultDic;
            }
        }
        #endregion



        /// <summary>
        /// 将默认数据保存到文件
        /// </summary>
        public static void WriteDefault()
        {
            InsertDefault();
        }

        /// <summary>
        ///  将默认数据保存到 数据库存
        /// </summary>
        private static void InsertDefault()
        {
            try
            {
                foreach (var item in Permissions)
                {
                    if (PermissionDao.Instance.FindById(item.id) == null)
                    {
                        PermissionDao.Instance.Insert(item);
                    }
                }


                foreach (var item in Roles)
                {
                    if (RoleDao.Instance.FindById(item.id) == null)
                    {
                        RoleDao.Instance.Insert(item);
                    }
                }

                foreach (var item in RolePermissions)
                {
                    if (RolePermissionDao.Instance.FindById(item.id) == null)
                    {
                        RolePermissionDao.Instance.Insert(item);
                    }
                }

                foreach (var item in ScaleBrands)
                {
                    if (ScaleBrandDao.Instance.FindById(item.id) == null)
                    {
                        ScaleBrandDao.Instance.Sqlite.Insert(item);
                    }
                }

                foreach (var item in ScaleSeriesList)
                {
                    if (ScaleSeriesDao.Instance.FindById(item.id) == null)
                    {
                        ScaleSeriesDao.Instance.Sqlite.Insert(item);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("插入默认数据时出错：" + ex.Message);
                throw;
            }
        }
    }
}
